/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.storage.jdbc.sqlite;

import java.sql.ResultSet;
import java.sql.SQLException;

import de.fuberlin.replication.library.Metadata;
import de.fuberlin.replication.library.Version;

/**
 * <code>Iterator</code> implementation for {@link Metadata} objects.
 */
public class SQLiteMetadataIterator extends SQLiteIterator<Metadata> {

	/**
	 * Constructor sets the ResultSet and iterator.
	 * 
	 * @param res ResultSet to iterate over
	 */
	public SQLiteMetadataIterator(ResultSet res) {
		super(res);
		setIterator();
	}

	@Override
	public void setIterator() {
		ResultSet res = getResultSet();
		try {
			while (res.next()) {
				Metadata metadata = new Metadata();
				metadata.setName(res.getString(1));
				metadata.setVersion(new Version(res.getString(2), res.getLong(3)));
				metadata.setTimestamp(res.getLong(4));
				getList().add(metadata);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		setIterator(getList().iterator());
	}
}